# Makefile for sysu-giep

# 变量定义
APP_NAME=sysu-giep
BUILD_DIR=build
MAIN_PATH=cmd/server/main.go
CONFIG_PATH=configs/config.yaml

# Go相关变量
GO=go
GOOS?=$(shell go env GOOS)
GOARCH?=$(shell go env GOARCH)
GO_VERSION=1.24

# 版本信息
VERSION?=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")

# 构建标志
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"

.PHONY: help build run dev test clean swagger docker-build docker-run up-local down-local air

# 默认目标
help:
	@echo "可用的命令:"
	@echo "  build        - 构建应用"
	@echo "  run          - 运行应用"
	@echo "  test         - 运行测试"
	@echo "  clean        - 清理构建文件"
	@echo "  swagger      - 生成Swagger文档"
	@echo "  docker-build - 构建Docker镜像"
	@echo "  docker-run   - 运行Docker容器"
	@echo "  up-local     - 运行docker-compose-local服务"
	@echo "  down-local   - 停止docker-compose-local服务"
	@echo "  install      - 安装依赖"
	@echo "  fmt          - 格式化代码"
	@echo "  lint         - 代码检查"
	@echo "  air          - 使用air进行热重载运行"

# 安装依赖
install:
	@echo "安装依赖..."
	$(GO) mod download
	$(GO) mod tidy

# 格式化代码
fmt:
	@echo "格式化代码..."
	$(GO) fmt ./...

# 代码检查
lint:
	@echo "代码检查..."
	golangci-lint run

# 构建应用
build:
	@echo "构建应用..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH)

# 运行应用
run:
	@echo "运行应用..."
	$(GO) run $(MAIN_PATH)

# 运行测试
test:
	@echo "运行测试..."
	$(GO) test -v ./...

# 运行测试并生成覆盖率报告
test-coverage:
	@echo "运行测试并生成覆盖率报告..."
	$(GO) test -v -coverprofile=coverage.out ./...
	$(GO) tool cover -html=coverage.out -o coverage.html

# 清理构建文件
clean:
	@echo "清理构建文件..."
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# 生成Swagger文档
swagger:
	@echo "生成Swagger文档..."
	swag init -g $(MAIN_PATH) -o docs

# 构建Docker镜像
docker-build:
	@echo "构建Docker镜像..."
	docker build -t $(APP_NAME):$(VERSION) .

# 运行Docker容器
docker-run:
	@echo "运行Docker容器..."
	docker run -p 8080:8080 -v $(PWD)/configs:/app/configs $(APP_NAME):$(VERSION)

# 运行docker-compose-local服务
up-local:
	@echo "运行docker-compose-local服务"
	@docker-compose -f docker-compose.local.yml up --build -d 

# 停止docker-compose-local服务
down-local:
	@echo "停止docker-compose-local服务"
	@docker-compose -f docker-compose.local.yml down 

# 开发模式运行
dev:
	@echo "开发模式运行..."
	$(GO) run $(MAIN_PATH) --config $(CONFIG_PATH)

# 生产模式构建
build-prod:
	@echo "生产模式构建..."
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(LDFLAGS) -a -installsuffix cgo -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH)

# 检查Go版本
check-go-version:
	@echo "检查Go版本..."
	@$(GO) version | grep -q "go$(GO_VERSION)" || (echo "需要Go版本 $(GO_VERSION)" && exit 1)

# 预构建检查
pre-build: check-go-version install fmt lint

# 完整构建流程
all: pre-build build test

# 数据库迁移
migrate:
	@echo "运行数据库迁移..."
	$(GO) run scripts/migrate.go

# 数据库种子数据
seed:
	@echo "插入种子数据..."
	$(GO) run scripts/seed.go

# 性能测试
bench:
	@echo "运行性能测试..."
	$(GO) test -bench=. -benchmem ./...

# 安全扫描
security-scan:
	@echo "安全扫描..."
	gosec ./...

# 依赖检查
deps-check:
	@echo "检查依赖..."
	$(GO) mod verify
	$(GO) list -m all

# 更新依赖
deps-update:
	@echo "更新依赖..."
	$(GO) get -u ./...
	$(GO) mod tidy

# 使用air进行热重载运行
air:
	@echo "使用air进行热重载运行..."
	@air -c air.toml
